package com.cj.kvstore.simple

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Parcel
import android.os.Parcelable
import android.util.Log
import android.widget.TextView
import com.cj.kvstore.simple.databinding.ActivityMainBinding
import com.cj.kvstore.KVStore

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        KVStore.initialize(this)
        val kvStore = KVStore.kvStoreWithID("kv_mmap.kv")

//        val start = System.currentTimeMillis()
//        for(i in 0 ..10000){
//            kvStore.putInt("age" + i,i)
//        }
//        val end = System.currentTimeMillis()
//        val time = end - start
//        println("----------耗时：${time}")

        //appendShow("----------age99：${kvStore.getInt("age99",-1)}")
        //kvStore.putInt("age99",-200)
        //kvStore.putInt("age99",300)
        //kvStore.putInt("age98",-400)


//        appendShow("----------age99：${kvStore.getInt("age99",-1)}")
//        appendShow("----------age98：${kvStore.getInt("age98",-1)}")
//
//        appendShow("----------age888：${kvStore.getInt("age888",-1)}")
//
//        appendShow("----------name：${kvStore.getString("name")}")
//        kvStore.putString("name","chen_jian")
//        appendShow("----------name：${kvStore.getString("name")}")
//        kvStore.putString("name",null)
//
//        appendShow("----------name：${kvStore.getString("name")}")
//        kvStore.putString("name","chen_jian02")
//        kvStore.putString("name","chen_jian02")
//        appendShow("----------name：${kvStore.getString("name")}")
//        appendShow("----------name2：${kvStore.getString("name2")}")

//        appendShow("----------weight：${kvStore.getDouble("weight",-1.0)}")
//        kvStore.putDouble("weight",77.77)
//        appendShow("----------weight：${kvStore.getDouble("weight",-1.0)}")
//
//        appendShow("----------marriage：${kvStore.getBoolean("marriage",false)}")
//        kvStore.putBoolean("marriage",true)
//        appendShow("----------marriage：${kvStore.getBoolean("marriage",false)}")

//        appendShow("----------bytes：${byteArrayToString(kvStore.getBytes("bytes"))}")
//        kvStore.putBytes("bytes",ByteArray(4).apply { this[0] = 1;this[1] = 10;this[2] = 19;this[3] = 108 })
//        kvStore.removeValueFromKey("bytes")
//        appendShow("----------bytes：${byteArrayToString(kvStore.getBytes("bytes")?.apply {
//            Log.e("KVStore","-------------bytes:${this}")
//        })}")
//        kvStore.putBytes("bytes",ByteArray(4).apply { this[0] = 2;this[1] = 20;this[2] = 29;this[3] = 109 })
//        appendShow("----------bytes：${byteArrayToString(kvStore.getBytes("bytes")?.apply {
//            Log.e("KVStore","-------------bytes:${this}")
//        })}")
//        appendShow("----------bytes：${byteArrayToString(kvStore.getBytes("bytes")?.apply {
//            Log.e("KVStore","-------------bytes:${this}")
//        })}")

//        appendShow("parcelable: ${kvStore.getParcelable("parcelable",DataParcelable::class.java)}")
//        appendShow("parcelable: ${kvStore.getParcelable("parcelable",DataParcelable::class.java)}")
//        kvStore.putParcelable("parcelable",DataParcelable("chen",18))
//        appendShow("parcelable: ${kvStore.getParcelable("parcelable",DataParcelable::class.java)}")
//        appendShow("parcelable: ${kvStore.getParcelable("parcelable",DataParcelable::class.java)}")
//        appendShow("parcelable error: ${kvStore.getSerializable("parcelable",DataSerializable::class.java)}")

//        appendShow("serializable2: ${kvStore.getSerializable<DataSerializable>("serializable2",DataSerializable("南京",99.99))}")
//        kvStore.putSerializable("serializable",DataSerializable("上海",88.99))
//        appendShow("serializable: ${kvStore.getSerializable<DataSerializable>("serializable")}")
//        appendShow("serializable: ${kvStore.getSerializable<DataSerializable>("serializable")}")
//        appendShow("serializable error: ${kvStore.getParcelable("serializable",DataParcelable::class.java)}")
//        appendShow("serializable error: ${kvStore.getParcelable("serializable",DataParcelable::class.java)}")
//        appendShow("serializable: ${kvStore.getSerializable<DataSerializable>("serializable")}")


        kvStore.putSerializable("serializable",DataSerializable("上海",88.99))
        appendShow("serializable: ${kvStore.getSerializable<DataSerializable>("serializable")}")
        kvStore.putDouble("weight",77.77)
        appendShow("weight：${kvStore.getDouble("weight",-1.0)}")
        kvStore.clearAll()
        appendShow("serializable: ${kvStore.getSerializable<DataSerializable>("serializable")}")
        appendShow("weight：${kvStore.getDouble("weight",-1.0)}")

        kvStore.putSerializable("serializable",DataSerializable("上海",88.89))
        appendShow("serializable: ${kvStore.getSerializable<DataSerializable>("serializable")}")
        kvStore.putDouble("weight",77.77)
        appendShow("weight：${kvStore.getDouble("weight",-1.0)}")
        //kvStore.clearAll()
        appendShow("serializable: ${kvStore.getSerializable<DataSerializable>("serializable")}")
        appendShow("weight：${kvStore.getDouble("weight",-1.0)}")

        // Example of a call to a native method
        //KVStore().write()
        //binding.sampleText.text = KVStore(). read()
        //println("-------------path:${getExternalFilesDir(null)}")
    }

    private fun byteArrayToString(bytes:ByteArray?):String{
        if(bytes == null) return ""
        val sb = java.lang.StringBuilder()
        sb.append('[')
        bytes.forEach {
            sb.append(it).append(',')
        }
        sb.append(']')
        return sb.toString()
    }

    fun appendShow(text:String){
        binding.sampleText.text = binding.sampleText.text.toString() + "\n" + text
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    external fun stringFromJNI(): String

    companion object {
        // Used to load the 'native-lib' library on application startup.
        init {
            System.loadLibrary("native-lib")
        }
    }
}

data class DataSerializable(private var cityName:String,private var point:Double):java.io.Serializable

class DataParcelable(private var name:String?,private val age:Int) :Parcelable{
    constructor(parcel: Parcel) : this(
        parcel.readString(),
        parcel.readInt()
    ) {
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(name)
        parcel.writeInt(age)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<DataParcelable> {
        override fun createFromParcel(parcel: Parcel): DataParcelable {
            return DataParcelable(parcel)
        }

        override fun newArray(size: Int): Array<DataParcelable?> {
            return arrayOfNulls(size)
        }
    }

    override fun toString(): String {
        return "DataParcelable = [name = $name , age = $age]"
    }
}